module.exports = {
  Name: function (b) { //姓名等一般字符
    return [/^([\u2E80-\u9FFF]|\w|-|\(|\))*$/.test(b), '存在特殊字符'];
  },
  NoSpecial: function (b) {
    return [!/~|\^/.test(b), '存在非法字符'];
  },
  Phone: function (t) { //手机号码
    return [/^1\d{10}$/.test(t), '格式错误'];
  },
  IDCard: function (t) { //身份证号码
    if(!t) return [false,'为空']
    var s = t.length,
      b, i, w, m, d;
    if (s === 15) {
      b = t.match(/^(?:\d{6})(\d{2})(\d{2})(\d{2})(?:\d{3})$/);
      if (!b) {
        return [false, '格式错误'];
      }
      i = parseInt("19" + b[1], 10);
      w = parseInt(b[2], 10);
      m = parseInt(b[3], 10);
      d = t.charAt(s - 1) % 2
    } else {
      if (s === 18) {
        var u = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
          y = "10X98765432";
        for (var v = 0, c = 0; v < 17; v++) {
          c += t.charAt(v) * u[v]
        }
        if (y.charAt(c % 11) !== t.charAt(17).toUpperCase()) {
          return [false, '格式错误'];
        }
        b = t.match(/^(?:\d{6})(\d{4})(\d{2})(\d{2})(?:\d{3})(?:[0-9]|X)$/i);
        if (!b) {
          return [false, '格式错误'];
        }
        i = parseInt(b[1], 10);
        w = parseInt(b[2], 10);
        m = parseInt(b[3], 10);
        d = t.charAt(s - 2) % 2
      }
    }
    var x = new Date(i, w - 1, m);
    if (i !== x.getFullYear() || w !== x.getMonth() + 1 || m !== x.getDate()) {
      return [false, '格式错误'];
    }
    return [
      [i + (w < 10 ? "-0" : "-") + w + (m < 10 ? "-0" : "-") + m, i, w, m], '格式错误'
    ];
  },
  Email: function (t) { //邮箱
    return [/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(t), '格式错误'];
  },
  Date: function (b) { //日期
    return [/^\d{4}(-\d{1,2}(-\d{1,2})?)?$/.test(b), '格式错误'];
  },
  PositiveNum: function (n) { //正数 > 0
    return [/^\d+\.?\d*$/.test(n), '必须为正数'];
  },
  PositiveInt: function (n) { //正整数
    return [/^\d+$/.test(n), '必须为正整数'];
  },
  NotNull: function (b) { //不能为空
    if(typeof b == 'string') b = b.trim();
    return [!(b == null || b == undefined || b === ''), '不能为空'];
  },
  MaxLength: function (b, n) { //最大长度
    return [b.length <= n, '最大长度为' + n];
  },
  MinLength: function (b, n) {
    return [b.length >= n, '最小长度为' + n];
  },
  MaxNumber: function (b, n) {
    return [b <= n, '最大值为' + n];
  },
  MinNumber: function (b, n) {
    return [b >= n, '最小值为' + n];
  },
  NotSql: function (t) { //非SQL注入判定
    let reg = /\b(and\s+|information_schema|exec|insert|update|chr|mid|master|truncate|char|declare|delete|create|drop|md5|database|concat|extractvalue|%|etc|style|expression)\b/i;
    return [!reg.test(t), '存在风险字符'];
  },
  Regx: function (s, r) {
    r = r.split('_gmi_');
    return [new RegExp(r[0], r[1]).test(s), r[2] || '格式错误'];
  },
  Function: function (s, fn) {
    return fn(s);
  },
  Rules: function (value, rules, msg) {
    msg = msg || {};
    rules = rules || {};
    var err = [];
    if (!rules.NotNull && !this.NotNull(value)[0]) {
      //无需验证为空 但为空的情况
    } else {
      for (var k in rules) {
        var _rule = rules[k];
        if (_rule != false) {
          var v = this[k](value, _rule);
          if (!v[0]) {
            err.push(typeof msg[k] == 'string' ? msg[k] : v[1]);
            break;
          }
        }
      }
    }
    return [err.length == 0, err.join(',')];
  },
  ComponentsData: function (data, components, eachFunction) {
    var err = [];
    for (var i = 0; i < components.length; i++) {
      if (components[i].rule) {
        var v = this.Rules(data[components[i].id], components[i].rule);
        if (typeof eachFunction == 'function') eachFunction.call(components[i], v);
        if (!v[0]) err.push(components[i].label + v[1]);
      }
    }
    return [err.length == 0, err.join(',')];
  }
}